﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Main_Personal_Links : System.Web.UI.Page
{


    /*page defaults*/
    OfficeOnlineUser currentUser;

    private void AuthenticateUser()
    {
        try
        {
            currentUser = (OfficeOnlineUser)Session["CurrentUser"];
        }
        catch (Exception ex)
        {
            //Response.Redirect("../login.aspx?urlReferrer=" + Request.Url);
        }

        if (currentUser == null)
        {
            //Response.Redirect("../login.aspx?urlReferrer=" + Request.Url);
        }
    }
    /*end page defaults*/

    protected void Page_Load(object sender, EventArgs e)
    {
        AuthenticateUser();
        if (Request.QueryString["addlink"] == "1")
        {
            int result = AddUser();
            switch (result)
            {
                case 1:
                    form1.InnerHtml = "<response>" + "Success" + "</response>";
                    break;
                default:
                    form1.InnerHtml = "<response>" + "There was a problem trying to add the link, please try again or contact your administrator" + "</response>";
                    break;
            }
        }
        else if (Request.QueryString["editlink"] == "1")
        {
            int result = UpdateLink();
            switch (result)
            {
                case 1:
                    form1.InnerHtml = "<response>" + "Success" + "</response>";
                    break;
                default:
                    form1.InnerHtml = "<response>" + "There was a problem trying to update the link, please try again or contact your administrator" + "</response>";
                    break;
            }
        }
        else if (Request.QueryString["deletelink"] == "1")
        {
            int result = DeleteLink();
            switch (result)
            {
                case 1:
                    form1.InnerHtml = "<response>" + "Success" + "</response>";
                    break;
                default:
                    form1.InnerHtml = "<response>" + "There was a problem trying to delete the link, please try again or contact your administrator" + "</response>";
                    break;
            }
        }
        else
        {
            LoadLinks();
        }
    }

    private void LoadLinks()
    {
        string command = "sp_tb_links_GetUserLinks @userName='" + currentUser.UserName + "'";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        sqlCon.Open();
        SqlDataAdapter sda = new SqlDataAdapter(command, sqlCon);

        DataSet dsLinks = new DataSet();

        sda.Fill(dsLinks);

        sqlCon.Close();

        dgLinks.DataSource = dsLinks;

        dgLinks.DataBind();
    }

    protected void dgLinks_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                e.Item.Attributes.Add("onclick", "selectLinkTableElement('" + e.Item.ClientID + "')");
                e.Item.Attributes.Add("linkNo", DataBinder.Eval(e.Item.DataItem, "linkNo").ToString());
                e.Item.Cells[0].Style.Add("padding", "2px 0 2px 2%");
                e.Item.Cells[1].Style.Add("padding", "2px 0 2px 2%");
                e.Item.Cells[1].ForeColor = System.Drawing.Color.Blue;
                e.Item.Cells[1].Style.Add("text-decoration", "underline");
                e.Item.Cells[1].Style.Add("cursor", "pointer");
                e.Item.Cells[1].Attributes.Add("title", DataBinder.Eval(e.Item.DataItem,"linkDescription").ToString());
                
                break;
        }
    }

    private int AddUser()
    {

        string linkName = Request.QueryString["linkName"];
        string linkUrl = Request.QueryString["linkUrl"];
        string linkDescription = Request.QueryString["linkDescription"];

        string command = "sp_tb_links_InsertLink";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;


        sqlCmd.Parameters.Add("@userName", SqlDbType.VarChar, 50);
        sqlCmd.Parameters.Add("@linkName", SqlDbType.VarChar, 50);
        sqlCmd.Parameters.Add("@linkUrl", SqlDbType.VarChar, 255);
        sqlCmd.Parameters.Add("@linkDescription", SqlDbType.VarChar, 255);
        sqlCmd.Parameters.Add("@result", SqlDbType.Int, 16);
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@userName"].Value = currentUser.UserName;
        sqlCmd.Parameters["@linkName"].Value = linkName;
        sqlCmd.Parameters["@linkUrl"].Value = linkUrl;
        sqlCmd.Parameters["@linkDescription"].Value = linkDescription;

        sqlCmd.ExecuteNonQuery();

        int result = 0;

        try
        {
            result = Convert.ToInt16(sqlCmd.Parameters["@result"].Value);
        }
        catch (Exception ex)
        {
            result = 0;
        }

        sqlCon.Close();

        return result;  
    }


    private int UpdateLink()
    {
        string linkNo = Request.QueryString["linkNo"];
        string linkName = Request.QueryString["linkName"];
        string linkUrl = Request.QueryString["linkUrl"];
        string linkDescription = Request.QueryString["linkDescription"];
        

        string command = "sp_tb_links_UpdateLink";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        sqlCmd.Parameters.Add("@linkNo", SqlDbType.Int, 16);
        sqlCmd.Parameters.Add("@userName", SqlDbType.VarChar, 50);
        sqlCmd.Parameters.Add("@linkName", SqlDbType.VarChar, 50);
        sqlCmd.Parameters.Add("@linkUrl", SqlDbType.VarChar, 255);
        sqlCmd.Parameters.Add("@linkDescription", SqlDbType.VarChar, 255);
        sqlCmd.Parameters.Add("@result", SqlDbType.Int, 16);
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@linkNo"].Value = linkNo;
        sqlCmd.Parameters["@userName"].Value = currentUser.UserName;
        sqlCmd.Parameters["@linkName"].Value = linkName;
        sqlCmd.Parameters["@linkUrl"].Value = linkUrl;
        sqlCmd.Parameters["@linkDescription"].Value = linkDescription;

        sqlCmd.ExecuteNonQuery();

        int result = 0;

        try
        {
            result = Convert.ToInt16(sqlCmd.Parameters["@result"].Value);
        }
        catch (Exception ex)
        {
            result = 0;
        }

        sqlCon.Close();

        return result;  
    }

    private int DeleteLink()
    {
        string linkNo = Request.QueryString["linkNo"];
        

        string command = "sp_tb_links_DeleteLink";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        sqlCmd.Parameters.Add("@linkNo", SqlDbType.Int, 16);
        sqlCmd.Parameters.Add("@userName", SqlDbType.VarChar, 50);
        sqlCmd.Parameters.Add("@result", SqlDbType.Int, 16);
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@linkNo"].Value = linkNo;
        sqlCmd.Parameters["@userName"].Value = currentUser.UserName;
        
        sqlCmd.ExecuteNonQuery();

        int result = 0;

        try
        {
            result = Convert.ToInt16(sqlCmd.Parameters["@result"].Value);
        }
        catch (Exception ex)
        {
            result = 0;
        }

        sqlCon.Close();

        return result;  
    }
}
